package com.maoye.ossp.biz.statement.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.hotent.base.api.BaseModel;
import com.hotent.base.api.query.Direction;
import com.hotent.base.api.query.FieldSort;
import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.hotent.base.core.util.string.StringUtil;
import com.hotent.base.db.model.DefaultQueryFilter;
import com.hotent.base.db.mybatis.domain.DefaultFieldSort;
import com.hotent.base.db.mybatis.domain.DefaultPage;
import com.maoye.ossp.biz.brand.entity.Supplier;
import com.maoye.ossp.biz.brand.service.ISupplierService;
import com.maoye.ossp.biz.statement.entity.BizStatementPayinDetail;
import com.maoye.ossp.biz.statement.entity.BizStatementPayinSlip;
import com.maoye.ossp.biz.statement.entity.StatementSalesSelfrun;
import com.maoye.ossp.biz.statement.entity.StatementStock;
import com.maoye.ossp.biz.statement.service.StatementStockService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.ossp.webserviceClient.PosPayWebService;

/**
 * 库存controller类
 * 
 * @author yanghao
 * 
 */
@Controller
@RequestMapping(value = "statement/statementStock/")
public class StatementStockController extends BaseController {

	@Resource
	private StatementStockService statementStockService;
	
	@Resource
	private ISupplierService supplierService;

	@RequestMapping(value = "list")
	public String list(StatementStock statementStock,
			HttpServletRequest request, Model model) {
		//获取登陆用户Id 
				Long userId=UserUtils.getCurrentUserId();
				//根据当前用户Id 如果是供应商 获取用户获取供应商信息
				Supplier supplier=supplierService.get(userId);
				QueryFilter queryFilter = getQuerFilter(request, statementStock);	
				//设置供应商编码
				if(supplier!=null){
					queryFilter.addFilter("supplierNo", supplier.getSupplierNo(), QueryOP.EQUAL);
				 }
		Page<StatementStock> page = (Page<StatementStock>) statementStockService
				.find(queryFilter);
		/*List<StatementStock> list = page.getList();
		if (list != null) {
			for (StatementStock stock : list) {
				JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
				factory.setServiceClass(PosPayWebService.class);
				factory.setAddress("http://202.105.115.147:9000/ossp-web/service/pospayWebService");
				PosPayWebService service = (PosPayWebService) factory.create();
				String result = service.getGoodsInfo(stock.getBarCode(),
						stock.getGoodsCode());// 商品条码 和商品编码
				Gson gson = new Gson();
				JSONObject a = new JSONObject(result);
				Object b = a.get("data");
				JSONArray array = new JSONArray(b.toString());
				for (int i = 0; i < array.length(); i++) {
					JSONObject json = (JSONObject) array.get(i);
					stock.setCommodityCode(String.valueOf(json.get("goodscode"))
							.substring(10, 18));
					stock.setBarCode(String.valueOf(json.get("barcode")));
					stock.setGoodsName(String.valueOf(json.get("goodsname")));
					stock.setGoodsModel(String.valueOf(json.get("goodsModel")));
				}
			}
		}*/

		model.addAttribute("page", page);
		model.addAttribute("statementStock", statementStock);
		return resultPage("/biz/statement/statementStock/list", model, request);
	}

	public StatementStockService getStatementStockService() {
		return statementStockService;
	}

	public void setStatementStockService(
			StatementStockService statementStockService) {
		this.statementStockService = statementStockService;
	}

}
